
const transport = require('../utils/email')
const schedule = require('node-schedule');
const puppeteer = require('puppeteer-core');
const axios = require('axios');
var islock = false
const init = () => {
    if (islock) {
        return
    }
    (async () => {
        islock = true
        const browser = await puppeteer.launch({
            headless: true, // 有头模式
            executablePath: 'C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe' // 指定浏览器路径
        });
        const page = await browser.newPage();
        await page.goto('http://m.jdgzf.net/#/Login');
        axios.post('http://out.jdgzf.net/api/token', {
            grant_type: 'renter_login',
            username: '304D0F26FAF026BC3EE97C7005BF496E5E94FC2F6A65FC137C2D3EED95C78D1B',
            password: 'E1F17ECC0F98F86259ED389CE9D93C06',
            loginType: 0,
        }, {
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded'
            }
        }).then(async res => {
            let token = res.data.access_token
            await page.evaluate((token) => {
                localStorage.setItem('token', token);
            }, token);
            await page.goto('http://m.jdgzf.net/#/QuerySequence');
            await page.waitForSelector('.van-cell');
            // //找到class为uflex-item 的第五个元素
            let element = await page.$('.van-radio--horizontal');
            if (element) {
                setTimeout(() => {
                    element.click();
                }, 1000);


                setTimeout(async () => {
                    let element2 = await page.$('.van-button');
                    if (element2) {
                        element2.click();


                        setTimeout(async () => {
                            let element3 = await page.$('span[style="font-size:20px; font-weight:bold; color:red;"]');
                            if (element3) {
                                let text = await element3.evaluate(el => el.textContent);
                                console.log(text)
                                await browser.close();
                                await transport.sendMail({
                                    from: '1078118049@qq.com',
                                    to: list,
                                    subject: '公租房排队',
                                    text: `您的公租房当前排在第${text}名`
                                }).then(res => {
                                    islock = false
                                    // console.log(res)
                                })
                            }
                        }, 1000);

                    }
                    //span的style为font-size:20px; font-weight:bold; color:red; 我想取到它的text 通过style取到



                }, 6000);

            } else {
                console.log('没有找到元素')
            }
        })
        // await page.screenshot({ path: 'example.png' });

        // await browser.close();

    })();
}
const list = ['3289391259@qq.com', '2954020052@qq.com'];
const startScheduler = async () => {
    schedule.scheduleJob('0 0 */6 * * *', async () => {
        init()
    })
    console.log('公租房定时任务执行')
}
module.exports = {
    startScheduler
}
